/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package WebService;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.StringReader;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.jws.WebService;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import org.jdom.Document;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;

/**
 *
 * @author Moraima Joves
 */
@WebService(serviceName = "Respaldo")
public class Respaldo {

    /**
     * Servicio que recibe los datos del respaldo en xml
     */
    @WebMethod(operationName = "Respaldar")
    public void Respaldar(@WebParam(name = "xml")
    String xml) throws JDOMException, IOException {
        FileOutputStream file_os = null;
        try {
            Properties prop = new Properties();
            FileInputStream is=new FileInputStream("Configuracion.properties");
            prop.load(is);
            /*guardamos los cambios*/
            SAXBuilder builder = new SAXBuilder();
            SimpleDateFormat sdf=new java.text.SimpleDateFormat("dd-MM-yyyy--H mm");
            String fecha[] = sdf.format(new Date()).split(" ");
            String nombre = fecha[0] + "h " + fecha[1] + "min";
            Document result = builder.build(new StringReader(xml));
            XMLOutputter xml_out=new XMLOutputter();
            File directorio=new File(prop.getProperty("RutaArchivosRespaldo")); 
            if(!directorio.exists())
                directorio.mkdir(); 
            String ruta = prop.getProperty("RutaArchivosRespaldo") + "/" + nombre;
            File fil = new File(ruta);
            file_os = new FileOutputStream(fil);
            xml_out.output(result,file_os);
            file_os.close();
        } catch (FileNotFoundException ex) {
            Logger.getLogger(Respaldo.class.getName()).log(Level.SEVERE, null, ex);
        } catch(Exception ex){
            Logger.getLogger(Respaldo.class.getName()).log(Level.SEVERE, null, ex);
            
        }
finally {
            try {
                file_os.close();
            } catch (IOException ex) {
                Logger.getLogger(Respaldo.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

    }

    /**
     * Servicio que obtiene la lista de archivos de respaldo
     * Web service operation
     */
    @WebMethod(operationName = "ObtenerListaRespaldo")
    public List<String> ObtenerListaRespaldo() {
        List<String> retorno = new ArrayList<String>();
                
        try {
            Properties prop = new Properties();
            FileInputStream is=new FileInputStream("Configuracion.properties");
            prop.load(is);
            File directorio = new File(prop.getProperty("RutaArchivosRespaldo"));
            for (File file : directorio.listFiles()) {
                 retorno.add(file.getName());
            }
            return retorno;
            
        } catch (Exception e) {
            
            System.out.println(e);
            return retorno;
        }
    }

    /**
     * Servicio que obtiene el xml del respaldo solicitado
     * Web service operation
     */
    @WebMethod(operationName = "ObtenerRespaldo")
    public String ObtenerRespaldo(@WebParam(name = "respaldo")
    String respaldo) {
        try { 
            String docNuevoStr = "";
            Properties prop = new Properties();
            FileInputStream is=new FileInputStream("Configuracion.properties");
            prop.load(is);
            SAXBuilder builder = new SAXBuilder();
            Document result = builder.build(new FileInputStream(
                    prop.getProperty("RutaArchivosRespaldo") + "/" + respaldo ));
            
            Format format = Format.getPrettyFormat();  
            XMLOutputter xmloutputter = new XMLOutputter(format);
            docNuevoStr = xmloutputter.outputString(result); 
            
        return docNuevoStr;
        } catch (Exception e) {
            
        return "";
        }
    }
    

    

}
